#include "UdpServe.hpp"

// ./client 127.0.0.1 8899
int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        LOG(FATAL, "./client client-ip client-port\n");
        exit(1);
    }
    std::string ip = argv[1];
    uint16_t port = std::stoi(argv[2]);

    int sockfd = socket(AF_INET, SOCK_DGRAM, 0);

    LOG(DEBUG, "[client] [%s] [%d]\n", ip.c_str(), port);
    sockaddr_in local;
    local.sin_family = AF_INET;
    local.sin_port = htons(port);
    local.sin_addr.s_addr = inet_addr(ip.c_str());
    sockaddr_in recv_net;
    memset(&recv_net, 0, sizeof(recv_net));
    socklen_t len = sizeof(recv_net);
    while (true)
    {
        std::string enter = "[enter message] :";
        std::cout << enter;
        std::string app;
        std::getline(std::cin, app);
        enter += app;
        sendto(sockfd, enter.c_str(), enter.size(), 0, (struct sockaddr *)&local, sizeof(local));

        char recv[1024] = {0};
        int n = recvfrom(sockfd, recv, sizeof(recv), 0, (struct sockaddr *)&recv_net, &len);
        std::cout << recv << std::endl;
    }

    return 0;
}